﻿using Pulog.Models;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace System.Web.Mvc
{

    public static class HtmlHelperExtensions
    {

        private static int commentLevel = 1;
        //private static int currentCommentCount = 1;
        private static bool isCurrentLevelOdd = true;

        /// <summary>
        /// Loop through the comment and display them and their children
        /// </summary>
        /// <param name="html">HtmlHelper</param>
        /// <param name="comments">The comments to be looped through and displayed</param>
        /// <param name="commentsParentID">The comments' parent ID</param>
        /// <returns>HTML code of comments</returns>
        public static string ThreadedComment(this HtmlHelper html,IEnumerable<Comment> comments,int? commentsParentID, bool isAuthenticated)
        {
            StringBuilder htmlOutput = new StringBuilder();

            if (comments.Count() > 0)
            {
                //bool isCurrentCommentOdd = true;
                //htmlOutput.Append("<ul>");
                foreach (Comment comment in comments)
                {
                    //if (currentCommentCount % 2 == 0)
                    //{
                    //    isCurrentCommentOdd = false;
                    //}
                    //else
                    //{
                    //    isCurrentCommentOdd = true;
                    //}
                    if (commentsParentID != null)
                    {
                        commentLevel += 1;
                    }
                    else
                    {
                        commentLevel = 1;
                    }
                    if (commentLevel % 2 == 0)
                    {
                        isCurrentLevelOdd = false;
                    }
                    else
                    {
                        isCurrentLevelOdd = true;
                    }
                    //htmlOutput.Append("<li>");
                    
                    htmlOutput.AppendFormat("<div id=\"Comment{0}\" ", comment.CommentID);
                    if (comment.CommentParentID == null && isCurrentLevelOdd)
                    {
                        htmlOutput.Append("class=\"commentParent oddComment\">");
                    }
                    else if (comment.CommentParentID == null && !isCurrentLevelOdd)
                    {
                        htmlOutput.Append("class=\"commentParent evenComment\">");
                    }
                    else if (comment.CommentParentID != null && isCurrentLevelOdd)
                    {
                        htmlOutput.Append("class=\"commentChild oddComment\">");
                    }
                    else if (comment.CommentParentID != null && !isCurrentLevelOdd)
                    {
                        htmlOutput.Append("class=\"commentChild evenComment\">");
                    }
                    htmlOutput.AppendFormat("<div class=\"postMisc\"><a name=\"Comment{0}\">Posted by: </a>",comment.CommentID);
                    if (!string.IsNullOrEmpty(comment.AuthorURL) && comment.AuthorURL != "http://")
                    {
                        htmlOutput.AppendFormat("<a href=\"{0}\">{1}</a>",comment.AuthorURL,HttpUtility.HtmlEncode(comment.AuthorName));
                    }
                    else{
                        htmlOutput.Append(HttpUtility.HtmlEncode(comment.AuthorName));
                    }
                    htmlOutput.AppendFormat(" 发表于: {0} | {1}<br /></div>",comment.CommentTime,Helper.GetDateDifference(comment.CommentTime));
                    htmlOutput.AppendFormat("<div class=\"commentContent\">{0}<br /></div>", comment.CommentContent);
                    // if is Admin
                    if(isAuthenticated)
                    {
                        htmlOutput.AppendFormat("IP: {0} | ", comment.AuthorIP);
                        htmlOutput.AppendFormat("<a id=\"editComment\" href=\"..\\EditComment\\{0}\">Edit</a> | ",comment.CommentID);
                        htmlOutput.AppendFormat("<a id=\"deleteComment\" href=\"javascript:DeleteComment({0})\">Delete</a>",comment.CommentID);
                    } 
                    if (commentLevel <= Helper.MaxCommentLevel-2)
                    {
                        htmlOutput.AppendFormat("<div id=\"reply-comment{0}\"><a href=\"javascript:void(0);\" onclick=\"ReplyComment('#reply-comment{0}',{0});\">" + "Reply</a></div>", comment.CommentID);
                    }
                    
                    //currentCommentCount += 1;
                    //if(comment.ChildrenComments != null)
                        htmlOutput.Append(html.ThreadedComment(comment.Comments.OrderByDescending(c=>c.CommentTime),comment.CommentID,isAuthenticated));
                    //htmlOutput.Append("</li>");
                    htmlOutput.Append("</div>");                    
                }

                //htmlOutput.Append("</ul>");
                // If this comment is the root, set commentLevel to 0;
            }
            commentLevel -= 1;
            return htmlOutput.ToString();
        }
    }
}
